package com.xdap.fsapproval.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xdap.common.dao.ApprovalDefinitionConfigDao;
import com.xdap.common.pojo.dto.ApprovalInstanceLogsDto;
import com.xdap.common.pojo.dto.DataResponse;
import com.xdap.common.pojo.dto.webhook.WebhookProcessDto;
import com.xdap.common.pojo.dto.webhook.XdapProcInstanceDto;
import com.xdap.fsapproval.module.pojo.ApprovalInstanceLogs;
import com.xdap.fsapproval.pojo.ApprovalDefinitionConfig;
import com.xdap.fsapproval.service.AsagelApprovalService;
import com.xdap.fsapproval.service.AsagelSendApprovalSertvice;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @BelongsProject: com.adisseo.service.approval.service.impl
 * @Author: liuyang
 * @CreateTime: 2022/9/5 15:30
 * @Description:
 * @Version: 1.0
 */
@Service
@Slf4j
public class SendApprovalSertviceImpl implements AsagelSendApprovalSertvice {
    
    @Autowired
    AsagelApprovalService asagelApprovalService;
    
    @Autowired
    ApprovalDefinitionConfigDao approvalDefinitionConfigDao;
    
    @Override
    public DataResponse sendApproval(WebhookProcessDto webhookProcessDto) {
        log.info("webhook数据,json数据:"+ JSONObject.toJSONString(webhookProcessDto));
        //判断流程数据是否存在
        if (webhookProcessDto.getXdapProcInstance()!=null){
            XdapProcInstanceDto xdapProcInstance = webhookProcessDto.getXdapProcInstance();
            //判断是不是提交
            if ("SUBMIT".equals(xdapProcInstance.getButtonCode())){
                // 判断fromId是不是在配置表
                ApprovalDefinitionConfig feishu = approvalDefinitionConfigDao.getFeishu(xdapProcInstance.getFormId());
                if (feishu!=null){
                    //如果在配置表中，退出（暂时）
                }else {
                    //如果不在配置表中，创建飞书审批定义
                    ApprovalInstanceLogsDto approvalInstanceLogsDto = asagelApprovalService.defineApproval(xdapProcInstance);
                    //往配置表中添加记录
                    ApprovalDefinitionConfig approvalDefinitionConfig = new ApprovalDefinitionConfig();
                    approvalDefinitionConfig.setApaasFormId(xdapProcInstance.getFormId());
                    approvalDefinitionConfig.setStatus(xdapProcInstance.getStatus());
                    approvalDefinitionConfig.setOwner(xdapProcInstance.getOwner());
                    approvalDefinitionConfig.setCreatedBy(xdapProcInstance.getCreatedBy());
                    approvalDefinitionConfig.setLastUpdatedBy(xdapProcInstance.getLastUpdatedBy());
                    approvalDefinitionConfig.setObjectVersionNumber(xdapProcInstance.getObjectVersionNumber());
                    approvalDefinitionConfig.setTenantId(xdapProcInstance.getTenantId());
                    approvalDefinitionConfig.setAppId(xdapProcInstance.getAppId());
                    if (approvalInstanceLogsDto!=null){
                        approvalDefinitionConfig.setFeishuApprovalCode(approvalInstanceLogsDto.getCode());
                        approvalDefinitionConfig.setMenuName(approvalInstanceLogsDto.getMenuName());
                        approvalDefinitionConfig.setPcLink(approvalInstanceLogsDto.getPcLink());
                        approvalDefinitionConfig.setMobileLink(approvalInstanceLogsDto.getMobileLink());
                    }
                    log.info("approvalDefinitionConfig,{}",approvalDefinitionConfig);
                    approvalDefinitionConfigDao.insertapprovalDefinitionConfig(approvalDefinitionConfig);
                }
            }
                //发送审批
                asagelApprovalService.consolidationApprovalData(webhookProcessDto);
        }else  if (webhookProcessDto.getApaasProcNotificationLog()!=null){
            // 24-1.17 新增推送待阅已阅
            asagelApprovalService.consolidationReadData(webhookProcessDto);

        }

//        if(webhookProcessDto!=null&& webhookProcessDto.getApaasProcNotificationLog()!=null){
//            //发送飞书审批消息
//            asagelApprovalService.sendApprovalBot(webhookProcessDto);
//        }
//
        return DataResponse.ok("推送飞书审批实例完成");
    }

    @Override
    public DataResponse compensateSend(String documentId) {
        ApprovalInstanceLogs approvalInstanceLogs = approvalDefinitionConfigDao.getApprovalInstanceLogs(documentId);
        if(approvalInstanceLogs!=null && StringUtils.isNotBlank(approvalInstanceLogs.getWebhookText())){
            WebhookProcessDto webhookProcessDto=JSONObject.parseObject(approvalInstanceLogs.getWebhookText(),WebhookProcessDto.class);
            sendApproval(webhookProcessDto);
        }
        return DataResponse.ok("重新推送飞书审批完成!!!");
    }
}
